<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css" MEDIA=screen>
<title>Shop Management System</title>
<?php include('menu.php'); ?>
<?php
	if(isset($_SESSION['user_group_id'])){
		if($_SESSION['user_group_id']!=1) {
		echo "<script>window.location = '../index.php';</script>";
   		exit();
		}
	}
?>
</head>
<body>
<?php
include("../connect/connect.php");

//ลบข้อมูล
if($_GET["action"]==del) {

	$cQuery = mysql_query("SELECT category_id FROM product WHERE category_id = '".$_GET["id"]."' ");
	$cNum = mysql_num_rows($cQuery);
	
	if($cNum<=0){
		$Query = "DELETE FROM category WHERE category_id = '".$_GET["id"]."' ";
		$objQuery = mysql_query($Query);
		if(!$objQuery)
		{
			echo "Error Delete [".mysql_error()."]";
		}
	} else {
		$showc = "<script>alert('รหัสหมวดนี้ยังใช้งานอยู่กรุณาลบสินค้าที่ใช้หมวดนี้ออกก่อนทั้งหมด')</script>";
	}
}

//ค้นหาสินค้า
if($_POST["category_id_search"] != "" OR $_POST["category_name_search"] != "" OR $_POST["category_namehead_search"] != "") {
	
		$Query = "SELECT * FROM category WHERE 1 ";
	
	if($_POST["category_id_search"] != "") {
		$Query .= "AND category_id LIKE '%".$_POST["category_id_search"]."%' ";
	}
	if($_POST["category_namehead_search"] != "") {
		$Query .= "AND category_head LIKE '%".$_POST["category_namehead_search"]."%' ";
	}
	if($_POST["category_name_search"] != "") {
		$Query .= "AND category_name LIKE '%".$_POST["category_name_search"]."%' ";
	}
	
} else {
	$Query = "SELECT * FROM category ORDER BY category_id";
}
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>จัดการหมวดสินค้า</h1></div>
  <div class="content">
  <table class="list">
        <thead>
          <tr>
            <td width="1" style="text-align: center;"><span class="center">รหัสหมวดสินค้า</span></td>
            <td width="1" class="center">รหัสนำหน้าหมวดสินค้า</td>
            <td class="center">หมวดสินค้า</td>
            <td width="218" class="center">จัดการหมวดสินค้า</td>
          </tr>
        </thead>
        <tbody>
		<form action="<?php echo $_SERVER["PHP_SELF"];?>" name="formsearch" method="post" id="formsearch">
          <tr class="filter">
            <td align="center"><input name="category_id_search" type="text" id="category_id_search" style="text-align: center" /></td>
            <td align="center"><input name="category_namehead_search" type="text" id="category_namehead_search" style="text-align:center" /></td>
            <td align="center"><input name="category_name_search" type="text" id="category_name_search" style="text-align:center" /></td>
            <td align="center"><a onclick="formsearch.submit();" class="button"><span>ค้นหา</span></a></td>
          </tr>
		</form>
<?php
	while($row = mysql_fetch_array($objQuery))
		{
?>
          <tr>
            <td style="text-align: center;"><?php echo $row["category_id"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["category_head"];?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["category_name"];?></td>
            <td class="center">[ <a href="product_edit_category.php?id=<?php echo $row["category_id"];?>">แก้ไข</a> ] [ <a href="JavaScript:if(confirm('คุณต้องการยืนยันการลบข้อมูล ใช่หรือไม่?')==true){window.location='<?php echo $_SERVER["PHP_SELF"];?>?action=del&id=<?php echo $row["category_id"];?>';}">ลบ</a> ]</td>
          </tr>
<?php
		}
		echo $showc;
?>
        </tbody>
      </table>
  </div>
</div>
</body>
<?php mysql_close(); ?>
</html>
